import java.util.*;



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param m int整型
     * @param n int整型
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        int len = n - m ;
        ListNode newHead = head ;
        ListNode newHeadPrev = null ;
        while (m > 1) {
            newHeadPrev = newHead ;
            newHead = newHead.next ;
            m-- ;
        }

        if (newHeadPrev != null) {
             ListNode cur = newHead ;
            while (len > 0 ) {
                ListNode curNext= cur.next ;
                cur.next = curNext.next ;
                curNext.next = newHead ;
                newHead = curNext ;
                newHeadPrev .next =newHead ;
                len -- ;
            }

        } else {
           ListNode cur = head ;
            while (len > 0) {
             ListNode curNext = cur.next ;
             cur.next = curNext.next ;
             curNext.next = head ;
             head = curNext ;
                len -- ;
            }

        }


        return head ;
    }

}
